//
//  EmotionManager.swift
//  1120自定义键盘
//
//  Created by 江先铨 on 15/11/22.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit

//主要负责加载组织所有表情文件 工具类
class EmotionManager: NSObject {
    
    //总共有多少组
    lazy var packages = [EmotionPakeage]()
    
    override init() {
        
        super.init()
        
        loadPackages()
    }
    
    //加载分组表情
    func loadPackages() {
      //获取文件的路径
      let path = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: "Emoticons.bundle")
        // print(path)
       //将plist 文件 转换成字典
        let dict = NSDictionary(contentsOfFile: path!)!
        //print(dict)
        
        if let array = dict["packages"] as? [[String : AnyObject]] {
            //对数组中得元素进行遍历
            for item in array {
            //这个id 是文件名 表示是要加载那个文件夹得表情  获取字典中得id
             let id = item["id"] as! String
                
              loadGroupPackage(id)
            }
        }
    }
    
    private func loadGroupPackage(id : String) {
        //获取每个分组中得info.plist
//         print(id)
        //获取文件的路径 加载plist 的文件
     let path = NSBundle.mainBundle().pathForResource("Info", ofType: "plist", inDirectory: "Emoticons.bundle/\(id)")
//     print(path)
        //从文件中加载字典
      let  dict = NSDictionary(contentsOfFile: path!)!
//        print(dict)
        let name = dict["group_name_cn"] as! String
        let array = dict["emoticons"] as! [[String : String]]
        
        let p = EmotionPakeage(id: id, name: name, emotions: array)
         packages.append(p)

    }

}
